#include <iostream>
#include <vector>
#include <stack>
using namespace std;


vector<int> solve(vector<int>&heights,vector<int>&signals)
{
    int n = heights.size();
    vector<int>res(n,0);        // 需不需要初始化，各种花式越界访问
    if (n <= 1)         return res;

    // 一次遍历找到左右两侧第一个最大的值
    vector<int>left(n,-1);
    vector<int>right(n,-1);
    stack<int>s;
    
    for (int i = 0;i < n;i++)
    {
        while (!s.empty()&&heights[i] > heights[s.top()])
        {
            int top_index = s.top();
            s.pop();
        }
        left[i] = s.empty()?-1:s.top();
        s.push(i);
    }
    // 有点牛逼

    // 然后就是要进行结果的赋值了
    for (int i = 0;i < n;i++)
    {
        if (left[i] != -1)
        {
            res[left[i]] += signals[i];
        }
        if (right[i]!= -1)
        {
            res[right[i]] += signals[i];
        }
    }

    return res;
}

int main()
{
    vector<int>heights = {2,3,5,2,1,5};
    vector<int>signals = {1,2,1,0,1,2};

    vector<int>res = solve(heights,signals);
    for (int i:res)
    {
        cout<<i<<' ';
    }
    return 0;
}